/**
 * 
 * 结果页控制器
 *
 */
class ResultController extends Controller {
    private resultView: ResultView;
    private resultResLoaded = false;

    public constructor() {
        super();
    }
	
   /**
    * 初始化控制器, 具体操作在子类里实现
    */
    protected init(): void {
        super.init();
    }

    /**
     * 初始化事件和命令监听，具体操作在子类里实现
     */
    protected initListeners(): void {
        super.initListeners();
        this.addCommand(CMDEvent.OPEN_RESUlT,this.showResult,this);
        this.addCommand(CMDEvent.LOAD_RESULT_RES,this.resultResLoad, this);
    }

    /**
     * 显示结果页
     */
    private showResult() {
        //资源加载成功
        if(this.resultResLoaded){
            if(this.resultView == null) {
                this.resultView = new ResultView();
                this.resultView.addEventListener(CMDEvent.RESUlT_OPERATE,this.clickOperate,this);
            } else {
                this.resultView.showResult();
            }
            this.root.removeChildren();
            this.root.addChild(this.resultView);
        }else{
            //资源未加载成功
            this.dispatch(CMDEvent.SHOW_LOADING);
        }
    }
    
    private resultResLoad():void{
        this.resultResLoaded = true;
        var num = this.root.numElements;
        if(num >= 2 && this.root.getChildAt(num - 1) instanceof LoadingView) {
            this.dispatch(CMDEvent.OPEN_RESUlT);
        }
    }
    
    /**
     * 处理结果页点击
     * type=1 预约，2重玩，3分享
     */
    private clickOperate(event: egret.Event) {
        var type: number = event.data;
        switch(type) {
            case 1:
                //TODO
                window.open("http://fir.im/xwzdx","_blank");
                break;
            case 2:
                this.dispatch(CMDEvent.OPEN_QUESTION,0);
                break;
            case 3:
                window.open("http://weibo.com/2188237805/","_blank");
                break;
        }

    }

}
